package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjProductImage;
import com.zb.entity.ZjSpareImage;
import com.zb.mapper.ZjSpareImageMapper;
import com.zb.service.ZjSpareImageService;
import com.zb.vo.GoodsImageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 备件图片表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-15
 */
@Service
public class ZjSpareImageServiceImpl extends ServiceImpl<ZjSpareImageMapper, ZjSpareImage> implements ZjSpareImageService {

    // 新增、修改、删除备件图片
    @Override
    public void save(Long custAccId, Long spareId, List<GoodsImageVo> imageVos, List<Long> delImageIds) {
        if (null != delImageIds && !delImageIds.isEmpty()) {
            this.removeByIds(delImageIds);
        }
        if (null == imageVos || imageVos.isEmpty()) {
            return;
        }
        List<ZjSpareImage> adds = new ArrayList<>();
        List<ZjSpareImage> updates = new ArrayList<>();
        ZjSpareImage image = null;
        for (GoodsImageVo vo : imageVos) {
            image = new ZjSpareImage();
            BeanUtils.copyProperties(vo, image);
            if (null != image.getId()) {
                updates.add(image);
                continue;
            }
            image.setId(IdWorker.getId());
            image.setCustAccId(custAccId);
            image.setSpareId(spareId);
            adds.add(image);
        }
        if (!adds.isEmpty()) {
            this.saveBatch(adds);
        }
        if (!updates.isEmpty()) {
            this.updateBatchById(updates);
        }
    }

    // 根据备件ID集合获取备件图片
    @Override
    public List<ZjSpareImage> getImageBySpareIds(List<Long> spareIds) {
        if (null == spareIds || spareIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjSpareImage> wrapper = new QueryWrapper<>();
        wrapper.select("id", "spare_id", "image");
        wrapper.in("spare_id", spareIds);
        return this.list(wrapper);
    }

    // 根据备件ID获取备件图片
    @Override
    public List<ZjSpareImage> getBySpareId(Long spareId) {
        QueryWrapper<ZjSpareImage> wrapper = new QueryWrapper<>();
        wrapper.select("id", "image");
        wrapper.eq("spare_id", spareId);
        return this.list(wrapper);
    }

    // 根据备件ID删除备件图片
    @Override
    public void deleteBatchBySpareIds(List<Long> spareIds) {
        if (null == spareIds || spareIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjSpareImage> wrapper = new QueryWrapper<>();
        wrapper.in("spare_id", spareIds);
        this.remove(wrapper);
    }
}
